<template>
  <div class="font-size-switch">
    <!-- 字体大小调整按钮 -->
    <el-dropdown @command="handleFontSizeChange" trigger="click" placement="bottom-end">
      <el-button type="text" class="font-size-btn hidden-mobile" title="调整字体大小">
        <el-icon class="font-size-icon">
          <Document />
        </el-icon>
      </el-button>

      <template #dropdown>
        <el-dropdown-menu class="font-size-dropdown">
          <div class="font-size-header">
            <span class="header-title">选择字体大小</span>
          </div>

          <div class="font-size-list">
            <div v-for="size in fontSizeList" :key="size.value"
              :class="['font-size-item', { active: currentFontSize === size.value }]"
              @click="handleFontSizeChange(size.value)"
              :title="`字体大小: ${size.value}px`">
              <span class="size-label" :style="{ fontSize: size.value + 'px' }">
                {{ size.label }}
              </span>
              <span class="size-value">{{ size.value }}px</span>
              <div class="size-check" v-if="currentFontSize === size.value">
                <el-icon>
                  <Check />
                </el-icon>
              </div>
            </div>
          </div>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { Document, Check } from '@element-plus/icons-vue'
import {
  getAllFontSizes,
  getCurrentFontSize,
  setFontSize,
  onFontSizeChange
} from '@/utils/fontSize'

// 响应式数据
const currentFontSize = ref(getCurrentFontSize())
const fontSizeList = ref(getAllFontSizes())

/**
 * 字体大小变更处理
 */
const handleFontSizeChange = async (size) => {
  try {
    const success = setFontSize(size)
    if (success) {
      currentFontSize.value = size
    }
  } catch (error) {
    console.error('切换字体大小失败:', error)
  }
}

// 监听字体大小变更
let unsubscribe = null

onMounted(() => {
  // 监听字体大小变更事件
  unsubscribe = onFontSizeChange((detail) => {
    currentFontSize.value = detail.fontSize
  })
})

onUnmounted(() => {
  // 清理监听器
  if (unsubscribe) {
    unsubscribe()
  }
})
</script>

<style scoped>
.font-size-switch {
  display: inline-block;
}

.font-size-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  color: #ffffff !important;
  font-size: 14px;
  padding: 8px 12px;
  border-radius: 4px;
  transition: all 0.3s ease;
}

.font-size-btn:hover {
  background-color: rgba(255, 255, 255, 0.1);
  color: var(--theme-header-text-color, #ffffff);
}

.font-size-icon {
  font-size: 16px;
}

.font-size-text {
  font-size: var(--font-size-small);
  font-weight: 500;
}

/* 下拉菜单样式 */
.font-size-dropdown {
  min-width: 200px;
  padding: 0;
  border-radius: 12px;
  box-shadow: var(--theme-shadow-dark);
  background: var(--theme-bg-primary);
}

.font-size-header {
  padding: 16px 16px 12px;
  text-align: center;
  border-bottom: 1px solid var(--theme-border-light);
}

.header-title {
  font-size: var(--font-size-base);
  font-weight: 600;
  color: var(--theme-text-primary);
}

.font-size-list {
  padding: 8px 0;
  max-height: 240px;
  overflow-y: auto;
}

.font-size-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  border-bottom: 1px solid var(--theme-border-light);
}

.font-size-item:last-child {
  border-bottom: none;
}

.font-size-item:hover {
  background-color: var(--theme-bg-secondary);
}

.font-size-item.active {
  background-color: var(--theme-primary);
  color: #ffffff;
}

.size-label {
  font-weight: 500;
  flex: 1;
}

.size-value {
  font-size: var(--font-size-small);
  color: var(--theme-text-secondary);
  margin-left: 8px;
}

.font-size-item.active .size-value {
  color: rgba(255, 255, 255, 0.8);
}

.size-check {
  margin-left: 8px;
  color: #ffffff;
}

/* 滚动条样式 */
.font-size-list::-webkit-scrollbar {
  width: 4px;
}

.font-size-list::-webkit-scrollbar-track {
  background: var(--theme-bg-secondary);
  border-radius: 2px;
}

.font-size-list::-webkit-scrollbar-thumb {
  background: var(--theme-border-light);
  border-radius: 2px;
}

.font-size-list::-webkit-scrollbar-thumb:hover {
  background: var(--theme-text-secondary);
}
</style>
